package org.lvslb.web.controller;

import org.lvslb.db.dao.AccessDao;
import org.lvslb.db.dao.AdminDao;
import org.lvslb.db.pojo.AccessLog;
import org.lvslb.db.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by MelOnDev1 on 13/05/14.
 */
@Controller
public class AdminController {

    @Autowired
    private AccessDao accessDao;

    @Autowired
    private AdminDao adminDao;

    @RequestMapping(value="/login")
    public ModelAndView login(HttpServletRequest request) {

        return new ModelAndView("login.jsp");
    }

    @RequestMapping(value="/verify")
    public void verify(String userName,String userPassword,HttpServletRequest request) {

        Admin admin = adminDao.verify(userName,userPassword);

        if(admin!=null){
            request.getSession().setAttribute("admin",admin);
        }


    }


    @RequestMapping(value="/inserTest")
    public ModelAndView insertTest(HttpServletRequest request) {

        for(int i = 0 ;i<200000;i++){
            AccessLog log = new AccessLog("test"+i);
            accessDao.persist(log);
        }

        return new ModelAndView("insertTest.jsp","accessDao",accessDao);
    }

    @RequestMapping(value="/admin/addnew")
    public ModelAndView addAdminNew(HttpServletRequest request) {

        Admin admin = new Admin(Long.valueOf(request.getParameter("id")),request.getParameter("userName"),request.getParameter("userPassword"));
        adminDao.persist(admin);
        return new ModelAndView("test.jsp","adminDao",adminDao);
    }


    @RequestMapping(value="/deleteAllTest")
    public ModelAndView deleteAllTest(HttpServletRequest request) {

         accessDao.deleteAll();

        return new ModelAndView("insertTest.jsp","accessDao",accessDao);
    }
}
